import Button from "@/components/common/Button"
import { PiLightningFill, PiStopBold } from "react-icons/pi"
import { FiSend } from "react-icons/fi"
import TextareaAutoSize from "react-textarea-autosize"
import {useEffect, useState} from "react";
import {sendMessageUsingPost} from "@/services/maple/aiCharController";
import {message} from "antd";
import {useAppContext} from "@/components/AppContext";
import {useEventBusContext} from "@/components/Chat/EventBusContext";
import {ActionType} from "@/reducers/AppReducer";

export default function ChatInput() {
    const [messageText, setMessageText] = useState("")
    const initSearchParams = {
      chatId: 'gpt-3.5-turbo',
      message: "",
    };
    const [addMessage, setaddMessage] = useState<API.AiChatMessage>({ ...initSearchParams });
    const {
      state: { messageList, currentModel, streamingId, selectedChat },
      dispatch
    } = useAppContext()
    // const { publish, subscribe, unsubscribe } = useEventBusContext()
    const send = async (content: string) => {
    setaddMessage((prev) => ({
      ...prev,
      message: content,
    }));
  };

  useEffect(() => {
    const sendMessage = async () => {
      // console.log(addMessage.message)
      if (addMessage.message) {
        console.log(addMessage.message)
        const res = await sendMessageUsingPost(addMessage);
        console.log(res)
        if (!res?.data) {
          message.error("信息提交失败");
        } else {
          setMessageText("");
          dispatch({ type: ActionType.ADD_MESSAGE, message: res.data })
          console.log(res.data);
        }
      }
    };
    sendMessage();
  }, [addMessage]);

    return (
        <div className='absolute bottom-0 inset-x-0 bg-gradient-to-b from-[rgba(255,255,255,0)] from-[13.94%] to-[#fff] to-[54.73%] pt-[1rem] dark:from-[rgba(53,55,64,0)] dark:to-[#353740] dark:to-[58.85%]'>
            <div className='max-w-full mx-auto flex flex-col items-center px-4 space-y-4'>
                {/*{messageList.length !== 0 &&*/}
                {/*    (streamingId !== "" ? (*/}
                {/*        <Button*/}
                {/*            icon={PiStopBold}*/}
                {/*            variant='primary'*/}
                {/*            onClick={() => {*/}
                {/*                stopRef.current = true*/}
                {/*            }}*/}
                {/*            className='font-medium'*/}
                {/*        >*/}
                {/*            停止生成*/} ,
                {/*        </Button>*/}
                {/*    ) : (*/}
                {/*        <Button*/}
                {/*            icon={MdRefresh}*/}
                {/*            variant='primary'*/}
                {/*            onClick={() => {*/}
                {/*                resend()*/}
                {/*            }}*/}
                {/*            className='font-medium'*/}
                {/*        >*/}
                {/*            重新生成*/}
                {/*        </Button>*/}
                {/*    ))}*/}
                <div className='flex items-end w-full border border-black/10 dark:border-gray-800/50 bg-white dark:bg-gray-700 rounded-lg shadow-[0_0_15px_rgba(0,0,0,0.1)] py-4'>
                    <div className='mx-3 mb-2.5 text-primary-500'>
                        <PiLightningFill />
                    </div>
                    <TextareaAutoSize
                        className='outline-none flex-1 max-h-64 mb-1.5 bg-transparent text-black dark:text-white resize-none border-0'
                        placeholder='输入一条消息...'
                        rows={1}
                        value={messageText}
                        onChange={(e) => {
                            setMessageText(e.target.value)
                        }}
                    />
                    <Button
                        className='mx-3 !rounded-lg'
                        icon={FiSend}
                        disabled={
                            messageText.trim() === ""
                        }
                        variant='primary'
                        onClick={() => {
                            send(messageText)
                        }}
                    />
                </div>
            </div>
        </div>
    )
}
